package com.shanghaionstar.dealersorder;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.dealer.MaintenanceDealerOrdersEntity;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.dealersorder.adapter.DealerOrderRecordAdapter;
import com.shanghaionstar.dealersorder.bean.DealerOrderRecord;
import com.shanghaionstar.dealersorder.presenter.OrderRecordPresenter;
import com.shanghaionstar.dealersorder.view.OrderRecordView;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.weight.CommonNoDatasLayout;

import java.util.List;

/**
 * 经销商预约记录
 * Created by Yonglang Hong on 2016/8/11.
 */
@ContentView(R.layout.onstar_dealer_book_record)
public class OrderRecordActivity extends SwipeActivity implements OrderRecordView{

    @FindViewById
    ListView lsv_record;
    @FindViewById
    CommonNoDatasLayout cndl_noDatas;
    @FindViewById
    ProgressLayout pl_loading;

    DealerOrderRecordAdapter recordAdapter;

    OrderRecordPresenter recordPresenter;

    public OrderRecordPresenter getRecordPresenter() {
        if (recordPresenter == null){
            recordPresenter = new OrderRecordPresenter(getScheduler(),this);
            recordPresenter.setOrderView(this);
        }
        return recordPresenter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init(){
        loadRecords();
    }

    private void loadRecords(){
        showLoading();
        getRecordPresenter().loadRecords();
    }

    private void showLoading() {
        pl_loading.setVisibility(View.VISIBLE);
        lsv_record.setVisibility(View.GONE);
        cndl_noDatas.setVisibility(View.GONE);
    }

    @Override
    public void showRecordList(DealerOrderRecord record) {
        pl_loading.setVisibility(View.GONE);
        cndl_noDatas.setVisibility(View.GONE);
        lsv_record.setVisibility(View.VISIBLE);
        setAdapter(record.getOrders());
    }

    private void setAdapter(List<MaintenanceDealerOrdersEntity> records){
        if (recordAdapter == null) {
            recordAdapter = new DealerOrderRecordAdapter(records, this);
            lsv_record.setAdapter(recordAdapter);
        }
        recordAdapter.setDatas(records);
    }

    @Override
    public void showEmpty() {
        pl_loading.setVisibility(View.GONE);
        lsv_record.setVisibility(View.GONE);
        cndl_noDatas.setVisibility(View.VISIBLE);
        cndl_noDatas.setHintText(R.string.DR_INF002);
    }

    @Override
    public void showErr(Throwable throwable) {
        pl_loading.setVisibility(View.GONE);
        lsv_record.setVisibility(View.GONE);
        cndl_noDatas.setVisibility(View.VISIBLE);
        cndl_noDatas.showIcon(true);
        cndl_noDatas.setHintText(R.string.no_set_meal_data_error);
        cndl_noDatas.setOnRefreshListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadRecords();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(recordPresenter != null){
            recordPresenter.finish();
            recordPresenter = null;
        }
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }
}
